/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pig.daemon.quartz.config;

import com.pig4cloud.pig.daemon.quartz.entity.SysJob;
import com.pig4cloud.pig.daemon.quartz.event.SysJobEvent;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.quartz.Trigger;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * 任务调用工厂
 * <p>
 * 负责发布任务执行事件，解耦任务调度和任务执行
 * 使用Spring事件机制实现任务的异步执行
 *
 * @author lengleng
 * @author 郑健楠
 * @date 2025/05/31
 */
@Slf4j
@Aspect
@Service
@AllArgsConstructor
public class PigQuartzInvokeFactory {

	/**
	 * Spring事件发布器
	 */
	private final ApplicationEventPublisher publisher;

	/**
	 * 初始化任务执行
	 * <p>
	 * 发布任务执行事件，由事件监听器处理具体的任务逻辑
	 * 实现任务调度与执行的解耦
	 *
	 * @param sysJob 任务实体
	 * @param trigger 触发器信息
	 */
	@SneakyThrows
	void init(SysJob sysJob, Trigger trigger) {
		// 发布任务执行事件
		publisher.publishEvent(new SysJobEvent(sysJob, trigger));
	}

}
